﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;
using System.Data;

namespace DQYT.HTDZ.Web.Views.SystemManage
{
    public partial class Dept : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            try
            {
                lblMsg.Text = "";
                lblPath.Text = "系统管理 >> 部门管理";
                if (!IsPostBack)
                {
                    if (Request["action"] == "add")
                    {
                        btnEdit.Visible = false;
                        InitData(false);
                    }
                    if (Request["action"] == "update")
                    {
                        btnAdd.Visible = false;
                        InitData(true);
                    }
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }
            #endregion
        }




        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                BDDepartmentBLL dBuMen = new BDDepartmentBLL();
                string strBUM_MC = txtBUM_MC.Text.Trim();
                if (strBUM_MC == string.Empty)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请填写部门名称')", true);
                    return;
                }
                else
                {
                    bool isCZ = dBuMen.CheckMC(strBUM_MC);
                    if (isCZ)
                    {
                        lblMsg.Text = "部门名称已经存在！";
                        return;
                    }
                }

                BDDepartmentModel mBuMen = new BDDepartmentModel();

                //int iMaxID = dBuMen.GetMaxSJID();
                mBuMen.BUM_ID = dBuMen.GetNextBUM_ID();
                mBuMen.BUM_MC = txtBUM_MC.Text.Trim();
                mBuMen.BUM_MS = "";
                mBuMen.BUM_RS = 0;
                mBuMen.BUM_SJID = Convert.ToInt32(ddlBUM_SJID.SelectedValue);
                mBuMen.BUM_DH = txtBUM_DH.Text;
                mBuMen.BUM_FZR = txtBUM_FZR.Text;
                mBuMen.BUM_BZ = txtBUM_BZ.Text;
                bool isOK = dBuMen.Add(mBuMen);
                if (isOK)
                {
                    Response.Redirect("DeptManage.aspx");
                }
                else
                {
                    lblMsg.Text = "保存失败！";
                    //JScript.Alert("保存失败！");
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
                //JScript.Alert("错误信息：" + ex.Message);
            }
            #endregion
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="isEdit">是否是编辑</param>
        private void InitData(bool isEdit)
        {
            #region
            //初始化上级单位
            ddlBUM_SJID.Items.Clear();
            BDDepartmentBLL dBuMen = new BDDepartmentBLL();
            DataTable dt = dBuMen.GetList("bum_sjid<=0").Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == 0)
                {
                    ListItem li1 = new ListItem("顶级部门", "0");
                    ddlBUM_SJID.Items.Add(li1);
                }
                ListItem li = new ListItem(dt.Rows[i]["BUM_MC"].ToString(), dt.Rows[i]["BUM_ID"].ToString());
                ddlBUM_SJID.Items.Add(li);
            }
            ddlBUM_SJID.SelectedValue = "0";
            //初始化基本信息
            if (isEdit)
            {

                BDDepartmentModel mBuMen = new BDDepartmentModel();
                mBuMen = dBuMen.GetModel(Convert.ToInt32(Request["id"].ToString()));
                ddlBUM_SJID.SelectedValue = mBuMen.BUM_SJID.ToString();
                txtBUM_MC.Text = mBuMen.BUM_MC;
                txtBUM_FZR.Text = mBuMen.BUM_FZR;
                txtBUM_DH.Text = mBuMen.BUM_DH;
                txtBUM_RS.Text = mBuMen.BUM_RS.ToString();
                txtBUM_BZ.Text = mBuMen.BUM_BZ;
            }

            #endregion
        }

        //修改
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                BDDepartmentBLL dBuMen = new BDDepartmentBLL();
                string strBUM_MC = txtBUM_MC.Text.Trim();
                if (strBUM_MC == string.Empty)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请填写部门名称')", true);
                    return;
                }

                int iBUM_ID = Convert.ToInt32(Request["id"].ToString());
                BDDepartmentModel mBuMen = new BDDepartmentModel();
                mBuMen = dBuMen.GetModel(iBUM_ID);
                //mBuMen.BUM_ID = 0;
                mBuMen.BUM_MC = txtBUM_MC.Text.Trim();
                mBuMen.BUM_MS = "";
                //mBuMen.BUM_RS = 0;
                mBuMen.BUM_SJID = Convert.ToInt32(ddlBUM_SJID.SelectedValue);
                mBuMen.BUM_DH = txtBUM_DH.Text;
                mBuMen.BUM_FZR = txtBUM_FZR.Text;
                mBuMen.BUM_BZ = txtBUM_BZ.Text;
                bool isOK = dBuMen.Update(mBuMen);
                if (isOK)
                {
                    Response.Redirect("DeptManage.aspx");
                }
                else
                {
                    lblMsg.Text = "保存失败！";
                    //JScript.Alert("保存失败！");
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
                //JScript.Alert("错误信息：" + ex.Message);
            }
            #endregion

        }
    }
}